<?php
// +----------------------------------------------------------------------
// | 类型管理
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;


class CateController extends AdminBaseController
{
    public function initialize()
    {
		$this->deal_model = Db::name("Cate");
        parent::initialize();
    }

    /*墓区类型管理*/
	public function index(){
        //5为墓区类型
		$where = array(
			'd.type'		=> 5,
			'd.is_delete'	=> 0,
			'parent_id'		=> 0
		);
		$cate_name = $this->request->param("cate_name");

		// 查询结果列表
		$results = $this->deal_model->alias('d')
						->field('id,cate_name,add_time,sort_order,if_show,mark')
						->where($where)
                        ->where('cate_name','like','%'.$cate_name.'%')
						->order("sort_order ASC,id DESC")
            			->paginate(10);
		$page = $results->render();
		$data = $results->all();//转数组

        //子级列表
		foreach($data as $key => $vo){
		    unset($where);
			$where1['parent_id'] = $vo['id'];
            $data[$key]['sub'] = Db::name("Cate")->alias('d')
						//->field('id,cate_name,add_time,sort_order,if_show,mark')
						->where($where1)
						->order("sort_order ASC,id DESC")
            			->select();
		}

		// 页面传值
		$this->assign("page", $page);
		$this->assign("results", $data);
		return $this->fetch();
	}

    /*墓区类型管理--添加*/
	public function add(){
		$pid = $this->request->param('id', 0, 'intval');
		$this->assign('pid', $pid);
		// 查询父级分类
		$mark=input('request.mark','normal');
		$this->getParent($mark);
		return $this->fetch();
	}
    /*墓区类型管理--编辑*/
    public function edit(){
        $id = $this->request->param('id');
        $data =  $this->deal_model->where('id', $id)->find();
        $this->assign($data);
        // 查询父级分类
        $mark = $this->request->param('mark');
        $this->getParent($mark);
        return $this->fetch();
    }
    /*石材类型--删除*/
    public function catedelete(){

        $data = $this->request->param('id');
        $cate =  $this->deal_model->where('id', $data)->find();
        if(empty($cate)){
            $this->error("没有找到该数据");
        }
        if($cate['type'] == 6){
            // 石材类别时，判断该类别下有没有别的数据
            $stuff = Db::name("stuff")->where('cate_id', $data)->where('is_delete',0)->find();
            if(!empty($stuff)){
                $this->error("请先删除该分类下的材料");
            }
        }

        $result = $this->deal_model->update(['is_delete'=>1,'id'=>$data]);

        $this->success('删除成功',url('Cate/index'));
    }

	/*墓区类型管理--获取父级分类*/
	private function getParent($mark){
		$where = array(
			'type'		=> 5,
			'if_show'	=> 1,
			'is_delete'	=> 0,
			'parent_id'	=> 0,
			'mark'=>$mark
		);
		$cates = $this->deal_model->field('id,cate_name')->where($where)->order('sort_order ASC,id DESC')->select();
		$this->assign('cates', $cates);
	}
	public function product(){
		return $this->fetch();
	}

	/*类型管理*/
	public function goodsindex(){
	    //2为商品类型
        $where = array(
            'd.type'		=> 2,
            'd.is_delete'	=> 0,
            'parent_id'		=> 0
        );
        //取库房数据
        $results = $this->deal_model->alias('d')->where($where)->order('sort_order ASC,d.id DESC')->paginate(12);
        $this->assign("results",$results);
        // 页面传值
        $page = $results->render();
        $this->assign("page", $page);
        return $this->fetch();
	}
	/*类型管理*/
	public function addgoodscate(){
		$this->assign("pid",'0');
		$this->assign("cates",'');
		return $this->fetch();
	}
    public function addgoodscate_post(){
        $data = $this->request->param();
        $data['add_user'] = cmf_get_current_admin_id();
        $data['add_time'] = time();
        $data['last_time'] = time();
        $this->deal_model->insertGetId($data);
        $this->success("添加成功", url('Cate/goodsindex'));
    }
    /*类型管理*/
    public function editgoodscate(){
        $id = $this->request->param("id",0,'intval');
        $where = array(
            'id'	=> $id
        );
        $result = $this->deal_model->where($where)->find();
        $this->assign($result);
        return $this->fetch();
    }
    public function ediygoodscate_post(){
        if ($this->request->isPost()) {
            $arrData  = $this->request->post();
            $arrData['last_time'] = time();
            $this->deal_model->update($arrData);
            $this->success(lang("EDIT_SUCCESS"), url('Cate/goodsindex'));
        }
    }


    /*石材类型管理*/
    public function stoneindex(){

        $where = array(
            'type'		=> 6,
            'is_delete'	=> 0,
            'parent_id'	=> 0,
            //'if_show'   => 1,
        );

        $cate_name = $this->request->param('cate_name');

        $results = $this->deal_model->where($where)
            ->where('cate_name','like','%'.$cate_name.'%')
            ->order('id','DESC')
            ->paginate(15);

        $page = $results->render();

        $this->assign('results',$results);
        $this->assign('page',$page);
        return $this->fetch();
    }

    /*石材类型--添加页面*/
    public function addstonecate(){

        return $this->fetch();
    }

    /*石材类型--添加保存*/
    public function addstonecate_post(){

        $data           = $this->request->param();
        $data['add_user'] = cmf_get_current_admin_id();
        $data['add_time'] =time();
        $data['last_time'] =time();

        $this->deal_model->insertGetId($data);
        $this->success('添加成功',url('Cate/stoneIndex'));
    }

    /*石材类型--编辑页面*/
    public function editstonecate(){

        $id = $this->request->param('id');

        $where = array(
            'id'    => $id
        );
        $result = $this->deal_model->where($where)->find();

        $this->assign($result);
        return $this->fetch();
    }

    /*石材类型--编辑保存*/
    public function editstonecate_post(){

        $data = $this->request->param();

        $this->deal_model->update($data);
        $this->success('修改成功',url('Cate/stoneIndex'));
    }

    /*石材类型--删除*/
    public function delete(){

        $data = $this->request->param('id');
        $cate =  $this->deal_model->where('id', $data)->find();
        if(empty($cate)){
            $this->error("没有找到该数据");
        }
        if($cate['type'] == 6){
            // 石材类别时，判断该类别下有没有别的数据
            $stuff = Db::name("stuff")->where('cate_id', $data)->where('is_delete',0)->find();
            if(!empty($stuff)){
                $this->error("请先删除该分类下的材料");
            }
        }
        $result = $this->deal_model->update(['is_delete'=>1,'id'=>$data]);

        $this->assign($result);
        $this->success('删除成功',url('Cate/stoneIndex'));
    }

}